﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Admin_CustomerManager : System.Web.UI.Page
{
    ThanhVienCtr thanhVienCtr = new ThanhVienCtr();
    TimKiemCtr timKiemCtr = new TimKiemCtr();
    DonDatHangCtr donDatHangCtr = new DonDatHangCtr();
    protected void checkAlreadyLogin()
    {
        if (Session["UserID"].ToString() == null||Session["QuanTri"].ToString()==null)
        {
            Response.Redirect("Login.aspx");
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            checkAlreadyLogin();
            MultiView1.ActiveViewIndex = 0;
            RegularExpressionValidator1.Visible = false;
            if (Request.QueryString["ChuoiTimKiem"] == "")
            {
            //gvKhachHang.DataSource = thanhVienCtr.GetThanhVien1H();
            //gvKhachHang.DataBind();
            }
            else
            {
                string tenThanhVien = Request.QueryString["ChuoiTimKiem"];
                gvKhachHang.DataSource = timKiemCtr.TimKiemThanhVienTheoTen(tenThanhVien);
                gvKhachHang.DataBind();
               
            }
        }

    }
    protected void gvKhachHang_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Edit")
            {
                string tenkh = e.CommandArgument.ToString();
                hdTenKH.Value = tenkh;
                MultiView1.ActiveViewIndex = 1;
                DataTable chiTietKhachHang = thanhVienCtr.layHoaDonTheoKhachHang(tenkh);
                if (chiTietKhachHang.Rows.Count > 0)
                {
                    gvChiTietKhangHang.DataSource = chiTietKhachHang;
                    gvChiTietKhangHang.DataBind();
                    Label1.Text = "";
                }
                else
                {
                    Label1.Text = "Không có hóa đơn nào!";
                }
            }
        }
        catch
        {
        }
    }
    protected void gvChiTietKhangHang_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvChiTietKhangHang.PageIndex = e.NewPageIndex;
        MultiView1.ActiveViewIndex = 1;
        gvChiTietKhangHang.DataSource = thanhVienCtr.layHoaDonTheoKhachHang(hdTenKH.Value);
        gvChiTietKhangHang.DataBind();
    }
    protected void gvKhachHang_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    protected void gvChiTietKhangHang_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    protected void gvChiTietKhangHang_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Edit")
            {
                int mahd = int.Parse(e.CommandArgument.ToString());
                hdmdhd.Value = mahd.ToString();
                MultiView1.ActiveViewIndex = 2;
                DataTable chiTietHoaDon=donDatHangCtr.LayDonDatHangH(mahd);
                if (chiTietHoaDon.Rows.Count > 0)
                {
                    gvChiTietHoaDon.DataSource = chiTietHoaDon;
                    gvChiTietHoaDon.DataBind();
                    lbCTHoaDon.Text = "";
                }
                else
                {
                    lbCTHoaDon.Text = "Không có chi tiết hóa đơn nào!";
                }
            }
        }
        catch
        {

        }
    }
    protected void gvChiTietHoaDon_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvChiTietHoaDon.PageIndex = e.NewPageIndex;
        MultiView1.ActiveViewIndex = 2;
        gvChiTietHoaDon.DataSource = donDatHangCtr.LayDonDatHang(int.Parse(hdmdhd.Value));
        gvChiTietHoaDon.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (txtSoCMND.Value == "")
        {
            DataTable tv = thanhVienCtr.GetThanhVien1H();
            if (tv.Rows.Count > 0)
            {
                gvKhachHang.DataSource = tv;
                gvKhachHang.DataBind();
                lbKH.Text = "";
            }
            else
            {
                lbKH.Text = "Không có khách hàng nào!";
            }
        }
        if (txtSoCMND.Value != "" && DropDownList1.SelectedValue=="1")
        {
            //RegularExpressionValidator2.Visible = false;
            DataTable tvcmnd=timKiemCtr.TimKiemThanhVienTheoCMND(txtSoCMND.Value);
            if (tvcmnd.Rows.Count > 0)
            {
                gvKhachHang.DataSource = tvcmnd;
                gvKhachHang.DataBind();
                lbKH.Text = "";
            }
            else
            {
                lbKH.Text = "Không có khách hàng nào!";
            }
        }
        if (txtSoCMND.Value != "" && DropDownList1.SelectedValue == "2")
        {
            //RegularExpressionValidator1.Visible = false;
            DataTable tvsdt=timKiemCtr.TimKiemThanhVienTheoSoDienThoai(txtSoCMND.Value);
            if(tvsdt.Rows.Count>0)
            {
                gvKhachHang.DataSource = tvsdt;
                gvKhachHang.DataBind();
                lbKH.Text = "";
            }
            else
            {
                lbKH.Text = "Không có khách hàng nào!";
            }
        }
        if (txtSoCMND.Value != "" && DropDownList1.SelectedValue == "3")
        {
            //RegularExpressionValidator1.Visible = false;
            DataTable tvten = timKiemCtr.TimKiemThanhVienTheoTen(txtSoCMND.Value);
            if (tvten.Rows.Count > 0)
            {
                gvKhachHang.DataSource = tvten;
                gvKhachHang.DataBind();
                lbKH.Text = "";
            }
            else
            {
                lbKH.Text = "Không có khách hàng nào!";
            }
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedValue == "1")
        {
            RegularExpressionValidator1.Visible = false;
            RegularExpressionValidator2.Visible = true;
            txtSoCMND.Value = "";
        }
        if (DropDownList1.SelectedValue == "2")
        {
            RegularExpressionValidator2.Visible = false;
            RegularExpressionValidator1.Visible = true;
            txtSoCMND.Value = "";
        }
        if (DropDownList1.SelectedValue == "3")
        {
            RegularExpressionValidator2.Visible = false;
            RegularExpressionValidator1.Visible = false;
            txtSoCMND.Value = "";
        }
    }
}